<?php
/**
 * golangshop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 杭州瑞璞玉数字科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.golangshop.com
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */

namespace addon\presale\model;

use addon\coupon\model\Coupon;
use addon\store\model\StoreGoodsSku;
use addon\store\model\StoreMember as StoreMemberModel;
use app\model\express\Config as ExpressConfig;
use app\model\express\Express;
use app\model\express\Local;
use app\model\order\Config;
use app\model\order\OrderCreate;
use app\model\order\OrderCreateTool;
use app\model\store\Store;
use app\model\system\Pay;
use think\facade\Cache;

/**
 * 订单创建(商品预售)
 *
 * @author Administrator
 *
 */
class PresaleOrderCreate extends OrderCreate
{

    use OrderCreateTool;
    private $goods_money = 0;//商品金额
    private $balance_money = 0;//余额
    private $delivery_money = 0;//配送费用
    private $coupon_money = 0;//优惠券金额
    private $adjust_money = 0;//调整金额
    private $invoice_money = 0;//发票费用
    private $promotion_money = 0;//优惠金额
    private $order_money = 0;//订金金额
    private $pay_money = 0;//支付总价
    private $final_money = 0;//尾款金额
    private $is_virtual = 0;  //是否是虚拟类订单
    private $order_name = '';  //订单详情
    private $goods_num = 0;  //商品种数
    private $member_balance_money = 0;//会员账户余额(计算过程中会逐次减少)
    private $pay_type = 'ONLINE_PAY';//支付方式
    private $invoice_delivery_money = 0;
    private $error = 0;  //是否有错误
    private $error_msg = '';  //错误描述
    private $recommend_member_card; // 推荐会员卡

    /************************************************** 定金支付 start *********************************************************************/

    /**
     * 订单创建
     * @param unknown $data
     */
    public function create($data)
    {
        //查询出会员相关信息
        $calculate_data = $this->depositCalculate($data);
        if (isset($calculate_data[ 'code' ]) && $calculate_data[ 'code' ] < 0)
            return $calculate_data;

        if ($this->error > 0) {
            return $this->error([ 'error_code' => $this->error ], $this->error_msg);
        }

        if (!empty($calculate_data[ 'invoice_type' ])) {
            if ($calculate_data[ 'invoice_type' ] == 1 && $calculate_data[ 'invoice_full_address' ] == "") {
                //物流,同城
                if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "express" || $calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "local") {
                    $calculate_data[ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
                    $calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
                }
                //门店
                if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "store") {
                    $delivery_store_info = json_decode($calculate_data[ 'shop_goods_list' ][ 'delivery_store_info' ], true);
                    $calculate_data[ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
                    $calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
                }
            }
        }

        $pay = new Pay();
        $out_trade_no = $pay->createOutTradeNo();

        $presale_common_order = new PresaleOrderCommon();
        $order_status_data = $presale_common_order->getOrderStatus();
        $order_status = $order_status_data[ 'data' ];

        model("promotion_presale_order")->startTrans();
        //循环生成多个订单
        try {

            $order_item = $calculate_data[ 'shop_goods_list' ]; //订单数据主体
            $item_delivery = $order_item[ 'delivery' ] ?? [];
            $delivery_type = $item_delivery[ 'delivery_type' ] ?? '';

            $site_id = $data[ 'site_id' ];
            $express_type_list = ( new \app\model\express\Config() )->getExpressTypeList($site_id);
            $delivery_type_name = $express_type_list[ $delivery_type ] ?? '';
            $presale_info = $calculate_data[ 'presale_info' ];
            $promotion_presale_info = $calculate_data[ 'promotion_presale_info' ];
            //订单主表
            $order_no = $this->createOrderNo($data[ 'site_id' ]);
            $order_type = $this->orderType($order_item, $calculate_data);

            $sku_info = $order_item[ 'goods_list' ][ 0 ];
            $data_order = [
                'site_id' => $data[ 'site_id' ],
                'site_name' => $sku_info[ 'site_name' ],
                'presale_id' => $data[ 'presale_id' ],
                'order_no' => $order_no,
                'deposit_out_trade_no' => $out_trade_no,
                'order_from' => $data[ 'order_from' ],
                'order_from_name' => $data[ 'order_from_name' ],
                'order_type' => $order_type[ 'order_type_id' ],
                'order_type_name' => $order_type[ 'order_type_name' ],
                'order_status_name' => $order_status[ $presale_common_order::ORDER_CREATE ][ 'name' ],
                'order_status_action' => json_encode($order_status[ $presale_common_order::ORDER_CREATE ], JSON_UNESCAPED_UNICODE),
                'pay_start_time' => $presale_info[ 'pay_start_time' ],
                'pay_end_time' => $presale_info[ 'pay_end_time' ],
                'is_fenxiao' => $presale_info[ 'is_fenxiao' ],
                'goods_id' => $presale_info[ 'goods_id' ],
                'goods_name' => $presale_info[ 'goods_name' ],

                'sku_id' => $data[ 'sku_id' ],
                'sku_name' => $sku_info[ 'sku_name' ],
                'sku_image' => $sku_info[ 'sku_image' ],
                'sku_no' => $sku_info[ 'sku_no' ],
                'is_virtual' => $sku_info[ 'is_virtual' ],
                'goods_class' => $sku_info[ 'goods_class' ],
                'goods_class_name' => $sku_info[ 'goods_class_name' ],
                'cost_price' => $sku_info[ 'cost_price' ],
                'sku_spec_format' => $sku_info[ 'sku_spec_format' ],

                'member_id' => $data[ 'member_id' ],
                'name' => $calculate_data[ 'member_address' ][ 'name' ] ?? '',
                'mobile' => $calculate_data[ 'member_address' ][ 'mobile' ] ?? '',
                'telephone' => $calculate_data[ 'member_address' ][ 'telephone' ] ?? '',
                'province_id' => $calculate_data[ 'member_address' ][ 'province_id' ] ?? '',
                'city_id' => $calculate_data[ 'member_address' ][ 'city_id' ] ?? '',
                'district_id' => $calculate_data[ 'member_address' ][ 'district_id' ] ?? '',
                'community_id' => $calculate_data[ 'member_address' ][ 'community_id' ] ?? '',
                'address' => $calculate_data[ 'member_address' ][ 'address' ] ?? '',
                'full_address' => $calculate_data[ 'member_address' ][ 'full_address' ] ?? '',
                'longitude' => $calculate_data[ 'member_address' ][ 'longitude' ] ?? '',
                'latitude' => $calculate_data[ 'member_address' ][ 'latitude' ] ?? '',
                'buyer_ip' => request()->ip(),
                'buyer_ask_delivery_time' => $order_item[ 'buyer_ask_delivery_time' ] ?? '',//定时达
                "buyer_message" => $order_item[ "buyer_message" ],

                'num' => $data[ 'num' ],
                'presale_deposit' => $presale_info[ 'presale_deposit' ],//定金单价
                'presale_deposit_money' => $calculate_data[ 'presale_deposit_money' ],//定金总额
                'presale_price' => $presale_info[ 'presale_price' ],//抵扣单价
                'presale_money' => $calculate_data[ 'presale_money' ],//抵扣总额

                'price' => $sku_info[ 'price' ],
                'goods_money' => $order_item[ 'goods_money' ],
                'balance_deposit_money' => $calculate_data[ 'balance_money' ],
                'pay_deposit_money' => $calculate_data[ 'pay_money' ],
                'delivery_money' => $calculate_data[ 'delivery_money' ],
                'promotion_money' => $calculate_data[ 'promotion_money' ],
                'coupon_id' => $order_item[ 'coupon_id' ] ?? 0,
                'coupon_money' => $calculate_data[ 'coupon_money' ] ?? 0,
                'invoice_money' => $calculate_data[ 'invoice_money' ],
                'final_money' => $calculate_data[ 'final_money' ],
                'order_money' => $calculate_data[ 'order_money' ],
                'delivery_type' => $delivery_type,
                'delivery_type_name' => $delivery_type_name,
                'delivery_store_id' => $item_delivery[ "store_id" ] ?? 0,
                "delivery_store_name" => $order_item[ "delivery_store_name" ] ?? '',
                "delivery_store_info" => $order_item[ "delivery_store_info" ] ?? '',

                "is_invoice" => $order_item[ "is_invoice" ] ?? 0,
                "invoice_type" => $order_item[ "invoice_type" ] ?? 0,
                "invoice_title" => $order_item[ "invoice_title" ] ?? '',
                "taxpayer_number" => $order_item[ "taxpayer_number" ] ?? '',
                "invoice_rate" => $order_item[ "invoice_rate" ] ?? 0,
                "invoice_content" => $order_item[ "invoice_content" ] ?? '',
                "invoice_delivery_money" => $calculate_data[ "invoice_delivery_money" ] ?? 0,
                "invoice_full_address" => $order_item[ "invoice_full_address" ] ?? '',
                'is_tax_invoice' => $order_item[ "is_tax_invoice" ] ?? '',
                'invoice_email' => $order_item[ "invoice_email" ] ?? '',
                'invoice_title_type' => $order_item[ "invoice_title_type" ] ?? 0,
                'order_status' => '0',
                'create_time' => time(),

                'is_deposit_back' => $promotion_presale_info[ 'is_deposit_back' ],
                'deposit_agreement' => $promotion_presale_info[ 'deposit_agreement' ],
            ];
            $order_id = model("promotion_presale_order")->add($data_order);

            //预售订单创建
            event('PresaleOrderCreate', [ 'id' => $order_id ]);

            //添加门店关注记录和减少门店商品库存 最新代码位置
            $result_list = $this->addStoreMemberAndDecStock($data_order);
            if ($result_list[ 'code' ] < 0) {
                model("promotion_presale_order")->rollback();
                return $result_list;
            }

            //减少库存
            $stock_result = $this->decStock([ "site_id" => $data[ 'site_id' ], "num" => $data[ 'num' ], 'presale_id' => $data[ 'presale_id' ], 'sku_id' => $data[ 'sku_id' ] ]);
            if ($stock_result[ "code" ] != 0) {
                model("promotion_presale_order")->rollback();
                return $stock_result;
            }

            //todo  满减送
            if (!empty($order_item[ 'manjian_rule_list' ])) {
                $mansong_data = [];
                foreach ($order_item[ 'manjian_rule_list' ] as $item) {
                    // 检测是否有赠送内容
                    if (isset($item[ 'rule' ][ 'point' ]) || isset($item[ 'rule' ][ 'coupon' ])) {
                        array_push($mansong_data, [
                            'manjian_id' => $item[ 'manjian_info' ][ 'manjian_id' ],
                            'site_id' => $order_item[ 'site_id' ],
                            'manjian_name' => $item[ 'manjian_info' ][ 'manjian_name' ],
                            'point' => isset($item[ 'rule' ][ 'point' ]) ? number_format($item[ 'rule' ][ 'point' ]) : 0,
                            'coupon' => $item[ 'rule' ][ 'coupon' ] ?? 0,
                            'coupon_num' => $item[ 'rule' ][ 'coupon_num' ] ?? '',
                            'order_id' => $order_id,
                            'member_id' => $data[ 'member_id' ],
                            'order_sku_ids' => !empty($item[ 'sku_ids' ]) ? implode($item[ 'sku_ids' ]) : '',
                        ]);
                    }
                }
                if (!empty($mansong_data)) {
                    model('promotion_mansong_record')->addList($mansong_data);
                }
            }

            //优惠券
            if ($data_order[ 'coupon_id' ] > 0 && $data_order[ 'coupon_money' ] > 0) {
                //优惠券处理方案
                $member_coupon_model = new Coupon();
                $coupon_use_result = $member_coupon_model->useCoupon($data_order[ 'coupon_id' ], $data[ 'member_id' ], $order_id); //使用优惠券
                if ($coupon_use_result[ 'code' ] < 0) {
                    model("promotion_presale_order")->rollback();
                    return $this->error('', "COUPON_ERROR");
                }
            }

            //扣除余额(统一扣除)
            if ($calculate_data[ "balance_money" ] > 0) {
                $calculate_data[ 'order_id' ] = $order_id;
                $this->pay_type = "BALANCE";
                $balance_result = $this->useBalance($calculate_data, $data[ 'site_id' ], 'presale_order');
                if ($balance_result[ "code" ] < 0) {
                    model("promotion_presale_order")->rollback();
                    return $balance_result;
                }
            }

            //添加门店关注记录和减少门店商品库存 原本代码位置

            //生成整体支付单据
            $pay->addPay($order_item[ 'site_id' ], $out_trade_no, $this->pay_type, $this->order_name, $this->order_name, $this->pay_money, '', 'DepositOrderPayNotify', '');
            //增加关闭订单自动事件
            $presale_order_model = new PresaleOrderCommon();
            $presale_order_model->addDepositOrderCronClose($order_id, $data[ 'site_id' ]);
            //如果退定金 增加尾款支付到期时间退定金操作
            if ($calculate_data[ 'is_deposit_back' ] == 0) {
                $presale_order_model->addRefundOrderCronClose($order_id, $data[ 'site_id' ], $calculate_data[ 'pay_end_time' ]);
            }

            Cache::tag("order_create_presale_" . $data[ 'member_id' ])->clear();
            model("promotion_presale_order")->commit();
            return $this->success($out_trade_no);
        } catch (\Exception $e) {
            model("promotion_presale_order")->rollback();
            return $this->error('', $e->getMessage());
        }

    }

    /**
     * 订单计算（定金）
     * @param unknown $data
     */
    public function depositCalculate($data)
    {
        $data = $this->initMemberAddress($data); //初始化地址
        $data = $this->initMemberAccount($data);//初始化会员账户

        //余额付款
        if ($data[ 'is_balance' ] > 0) {
            $this->member_balance_money = $data[ "member_account" ][ "balance_total" ] ?? 0;
        }

        //查询预售活动信息
        $promotion_presale_info = model('promotion_presale')->getInfo([ [ 'presale_id', '=', $data[ "presale_id" ] ] ]);

        if (empty($promotion_presale_info)) {
            $this->error = 1;
            $this->error_msg = "预售活动不存在!";
        }
        $data[ 'promotion_presale_info' ] = $promotion_presale_info;
        //查询预售信息
        $join = [
            [ 'promotion_presale pp', 'pp.presale_id = ppg.presale_id', 'inner' ],
            [ 'goods g', 'g.goods_id = ppg.goods_id', 'inner' ]
        ];
        $condition = [
            [ 'ppg.presale_id', '=', $data[ "presale_id" ] ],
            [ 'ppg.sku_id', '=', $data[ 'sku_id' ] ],
            [ 'g.goods_state', '=', 1 ],
            [ 'g.is_delete', '=', 0 ]
        ];
        $field = 'pp.*,ppg.sku_id,ppg.presale_stock,ppg.presale_deposit,ppg.presale_price,g.goods_name,g.is_virtual';
        $presale_info = model('promotion_presale_goods')->getInfo($condition, $field, 'ppg', $join);
        $this->is_virtual = $presale_info[ 'is_virtual' ];

        if (empty($presale_info)) {
            $this->error = 1;
            $this->error_msg = "商品不存在!";
        }
        //判断活动是否过期或开启
        if ($presale_info[ "status" ] != 1) {
            $this->error = 1;
            $this->error_msg = "当前商品预售活动未开启或已过期!";
        }
        //判断购买数是否超过限购
        if ($presale_info[ "presale_num" ] < $data[ 'num' ] && $presale_info[ "presale_num" ] > 0) {
            $this->error = 1;
            $this->error_msg = "该商品限制购买不能大于" . $presale_info[ "presale_num" ] . "件!";
        }

        //判断是否已存在订单
        $presale_order_count = model('promotion_presale_order')->getCount(
            [
                [ 'member_id', '=', $data[ 'member_id' ] ],
                [ 'presale_id', '=', $data[ 'presale_id' ] ],
                [ 'order_status', '>=', 0 ],
                [ 'refund_status', '=', 0 ]
            ]);
        if ($presale_order_count > 0) {
            $this->error = 1;
            $this->error_msg = "预售期间，同一商品只可购买一次!";
        }

        $data[ "presale_info" ] = $presale_info;

        //商品列表信息
        $shop_goods_list = $this->getOrderGoodsCalculate($data);
        $data[ 'shop_goods_list' ] = $this->shopOrderCalculate($shop_goods_list, $data);

        //定金金额
        $presale_deposit_money = $presale_info[ 'presale_deposit' ] * $data[ 'num' ];
        //余额抵扣(判断是否使用余额)
        if ($this->member_balance_money > 0) {
            if ($presale_deposit_money <= $this->member_balance_money) {
                $balance_money = $presale_deposit_money;
            } else {
                $balance_money = $this->member_balance_money;
            }
        } else {
            $balance_money = 0;
        }
        $pay_money = $presale_deposit_money - $balance_money;//计算出实际支付金额
        $this->pay_money += $pay_money;

        $this->member_balance_money -= $balance_money;//预减少账户余额
        $this->balance_money += $balance_money;//累计余额

        $order_money = $this->final_money + $presale_deposit_money;

        //总结计算
        $data[ 'delivery_money' ] = $this->delivery_money;
        $data[ 'coupon_money' ] = $this->coupon_money;
        $data[ 'adjust_money' ] = $this->adjust_money;
        $data[ 'invoice_money' ] = $this->invoice_money;
        $data[ 'invoice_delivery_money' ] = $this->invoice_delivery_money;
        $data[ 'promotion_money' ] = $this->promotion_money;
        $data[ 'presale_deposit_money' ] = $presale_deposit_money;
        $data[ 'order_money' ] = $order_money;
        $data[ 'balance_money' ] = $this->balance_money;
        $data[ 'pay_money' ] = $this->pay_money;
        $data[ 'goods_money' ] = $this->goods_money;
        $data[ 'goods_num' ] = $this->goods_num;
        $data[ 'is_virtual' ] = $this->is_virtual;
        $data[ 'final_money' ] = $this->final_money;
        $data[ 'presale_money' ] = $presale_info[ 'presale_price' ] * $data[ 'num' ];
        $data[ 'is_deposit_back' ] = $presale_info[ 'is_deposit_back' ];
        $data[ 'pay_end_time' ] = $presale_info[ 'pay_end_time' ];
        return $data;
    }

    /**
     * 待付款订单（定金）
     * @param unknown $data
     */
    public function depositOrderPayment($data)
    {
        $calculate_data = $this->depositCalculate($data);

        //优惠券
        $coupon_list = $this->getOrderCouponList($calculate_data);
        $calculate_data[ 'shop_goods_list' ][ "coupon_list" ] = $coupon_list;

        $express_type = [];
        if ($this->is_virtual == 0) {
            foreach ($calculate_data[ 'shop_goods_list' ][ 'deliver_sort' ] as $type) {
                // 物流
                if ($type == 'express' && $calculate_data[ 'shop_goods_list' ][ 'express_config' ][ 'is_use' ] == 1) {
                    $title = $calculate_data[ 'shop_goods_list' ][ 'express_config' ][ 'value' ][ 'express_name' ];
                    if ($title == '') {
                        $title = Express::express_type[ 'express' ][ 'title' ];
                    }
                    $express_type[] = [ 'title' => $title, 'name' => 'express' ];
                }
                // 自提
                if ($type == 'store' && $calculate_data[ 'shop_goods_list' ][ 'store_config' ][ 'is_use' ] == 1) {
                    //根据坐标查询门店
                    $store_model = new Store();
                    $store_condition = array (
                        [ 'site_id', '=', $data[ 'site_id' ] ],
                        [ 'is_pickup', '=', 1 ],
                        [ 'status', '=', 1 ],
                        [ 'is_frozen', '=', 0 ],
                    );
                    $latlng = array (
                        'lat' => $data[ 'latitude' ],
                        'lng' => $data[ 'longitude' ],
                    );
                    $store_list_result = $store_model->getLocationStoreList($store_condition, '*', $latlng);
                    $store_list = $store_list_result[ 'data' ];

                    $title = $calculate_data[ 'shop_goods_list' ][ 'store_config' ][ 'value' ][ 'store_name' ];
                    if ($title == '') {
                        $title = Express::express_type[ 'store' ][ 'title' ];
                    }
                    $express_type[] = [ 'title' => $title, 'name' => 'store', 'store_list' => $store_list ];
                }
                // 外卖
                if ($type == 'local' && $calculate_data[ 'shop_goods_list' ][ 'local_config' ][ 'is_use' ] == 1) {
                    //查询本店的通讯地址
                    $title = $calculate_data[ 'shop_goods_list' ][ 'local_config' ][ 'value' ][ 'local_name' ];
                    if ($title == '') {
                        $title = '外卖配送';
                    }
                    $store_model = new Store();
                    $store_condition = array (
                        [ 'site_id', '=', $data[ 'site_id' ] ],
                    );
                    if (addon_is_exit('store', $data[ 'site_id' ])) {
                        $store_condition[] = [ 'is_o2o', '=', 1 ];
                        $store_condition[] = [ 'status', '=', 1 ];
                        $store_condition[] = [ 'is_frozen', '=', 0 ];
                    } else {
                        $store_condition[] = [ 'is_default', '=', 1 ];
                    }
                    $latlng = array (
                        'lat' => $data[ 'latitude' ],
                        'lng' => $data[ 'longitude' ],
                    );
                    $store_list_result = $store_model->getLocationStoreList($store_condition, '*', $latlng);
                    $store_list = $store_list_result[ 'data' ];

                    $express_type[] = [ 'title' => $title, 'name' => 'local', 'store_list' => $store_list ];
                }
            }
        }

        $calculate_data[ 'shop_goods_list' ][ "express_type" ] = $express_type;
        $payment_event_data = event('OrderPayment', $calculate_data);

        if (!empty($payment_event_data)) {
            $calculate_data = array_merge($calculate_data, ...$payment_event_data);
        }

        return $calculate_data;
    }

    /**
     * 获取商品的计算信息
     * @param unknown $data
     */
    public function getOrderGoodsCalculate($data)
    {
        $shop_goods = $this->getPresaleShopGoodsList($data);
        $shop_goods[ 'promotion_money' ] = 0;
        $shop_goods_list = $shop_goods;
        return $shop_goods_list;
    }

    /**
     * 获取立即购买商品信息
     * @param $data
     * @return array
     */
    public function getPresaleShopGoodsList($data)
    {
        $join = [
            [ 'site ns', 'ngs.site_id = ns.site_id', 'inner' ]
        ];
        $field = 'sku_id, sku_name, sku_no, price, discount_price,cost_price, stock, volume, weight, sku_image, ngs.site_id, goods_state, is_virtual, is_free_shipping, shipping_template,goods_class, goods_class_name, goods_id, ns.site_name,sku_spec_format,goods_name,max_buy,min_buy,support_trade_type';
        $sku_info = model('goods_sku')->getInfo([ [ 'sku_id', '=', $data[ 'sku_id' ] ], [ "ngs.site_id", "=", $data[ "site_id" ] ] ], $field, 'ngs', $join);
        if (empty($sku_info)) {
            return $this->error([], "不存在的商品!");
        }
        $price = $sku_info[ "price" ];

        $sku_info[ 'num' ] = $data[ 'num' ];
        $goods_money = $price * $data[ 'num' ];
        $sku_info[ 'price' ] = $price;
        $sku_info[ 'goods_money' ] = $goods_money;
        $sku_info[ 'real_goods_money' ] = $goods_money;
        $sku_info[ 'coupon_money' ] = 0; //优惠券金额
        $sku_info[ 'promotion_money' ] = 0; //优惠金额
        $sku_info[ 'stock' ] = numberFormat($sku_info[ 'stock' ]);
        $goods_list[] = $sku_info;
        $shop_goods = [
            'goods_money' => $goods_money,
            'site_id' => $sku_info[ 'site_id' ],
            'site_name' => $sku_info[ 'site_name' ],
            'goods_list_str' => $sku_info[ 'sku_id' ] . ':' . $sku_info[ 'num' ],
            'goods_list' => $goods_list,
            'order_name' => $sku_info[ "sku_name" ],
            'goods_num' => $sku_info[ 'num' ],
            'limit_purchase' => [
                'goods_' . $sku_info[ 'goods_id' ] => [
                    'goods_id' => $sku_info[ 'goods_id' ],
                    'goods_name' => $sku_info[ "sku_name" ],
                    'num' => $sku_info[ 'num' ],
                    'max_buy' => $sku_info[ 'max_buy' ],
                    'min_buy' => $sku_info[ 'min_buy' ]
                ]
            ]
        ];
        if (isset($data[ 'delivery' ][ 'delivery_type' ]) && !empty($data[ 'delivery' ][ 'delivery_type' ]) && strpos($sku_info[ 'support_trade_type' ], $data[ 'delivery' ][ 'delivery_type' ]) === false) {
            $express_type_list = ( new \app\model\express\Config() )->getExpressTypeList($data[ 'site_id' ]);
            $delivery_type_name = $express_type_list[ $data[ 'delivery' ][ 'delivery_type' ] ] ?? '';
            $this->error = 1;
            $this->error_msg = '有商品不支持' . $delivery_type_name;
        }
        return $shop_goods;
    }

    /**
     * 库存变化
     * @return array
     */
    public function decStock($param)
    {
        $condition = array (
            [ 'site_id', '=', $param[ 'site_id' ] ],
            [ 'presale_id', '=', $param[ 'presale_id' ] ],
            [ 'sku_id', '=', $param[ 'sku_id' ] ]
        );
        $presale_info = model("promotion_presale_goods")->getInfo($condition, "presale_stock");
        if (empty($presale_info))
            return $this->error();

        if ($presale_info[ "presale_stock" ] <= 0)
            return $this->error('', "库存不足!");

        //编辑sku库存
        $res = model("promotion_presale_goods")->setDec($condition, "presale_stock", $param[ "num" ]);
        //减少总库存 2021.06.10
//        model("promotion_presale")->setDec(array(['site_id', '=', $param['site_id']],['presale_id', '=', $param['presale_id']],), "presale_stock", $param["num"]);
        if ($res === false)
            return $this->error();

        return $this->success($res);
    }

    /**
     * 获取店铺订单计算
     * @param unknown $site_id 店铺id
     * @param unknown $goods_money 商品总价
     * @param unknown $goods_list 店铺商品列表
     * @param unknown $data 传输生成订单数据
     */
    public function shopOrderCalculate($shop_goods, $data)
    {
        $site_id = $data[ 'site_id' ];

        //交易配置
        $config_model = new Config();
        $order_config_result = $config_model->getOrderEventTimeConfig($site_id);
        $order_config = $order_config_result[ "data" ];
        $shop_goods[ 'order_config' ] = $order_config[ 'value' ] ?? [];

        //定义计算金额
        $goods_money = $shop_goods[ 'goods_money' ];  //商品金额
        $delivery_money = 0;  //配送费用
        $promotion_money = 0;  //优惠费用（满减）
        $coupon_money = 0;     //优惠券费用
        $adjust_money = 0;     //调整金额
        $invoice_money = 0;    //发票金额
        $final_money = 0;      //尾款金额
        $balance_money = 0;    //会员余额
        $pay_money = 0;        //应付金额
        $order_money = 0;      //订单金额

        //实际抵扣金额
        if ($data[ 'presale_info' ][ 'presale_price' ] == 0) {//全款预售
            $deduction_money = 0;
        } else {
            $deduction_money = $data[ 'presale_info' ][ 'presale_price' ] * $data[ 'num' ] - $data[ 'presale_info' ][ 'presale_deposit' ] * $data[ 'num' ];
        }

        //计算邮费
        if ($this->is_virtual == 1) {
            //虚拟订单  运费为0
            $delivery_money = 0;
            $shop_goods[ 'delivery' ][ 'delivery_type' ] = '';
        } else {
            $express_config_model = new ExpressConfig();

            $deliver_type = $express_config_model->getDeliverTypeSort($site_id)[ 'data' ];
            $shop_goods[ 'deliver_sort' ] = explode(',', $deliver_type[ 'value' ][ 'deliver_type' ]);

            //查询店铺是否开启快递配送
            $shop_goods[ "express_config" ] = $express_config_model->getExpressConfig($site_id)[ 'data' ];

            //查询店铺是否开启门店自提
            $shop_goods[ "store_config" ] = $express_config_model->getStoreConfig($site_id)[ 'data' ];

            //查询店铺是否开启外卖配送
            $shop_goods[ "local_config" ] = $express_config_model->getLocalDeliveryConfig($site_id)[ 'data' ];

            //如果本地配送开启, 则查询出本地配送的配置
            if ($shop_goods[ "local_config" ][ 'is_use' ] == 1 && isset($data[ 'delivery' ][ 'store_id' ])) {
                $local_model = new Local();
                $local_info = $local_model->getLocalInfo([ [ 'site_id', '=', $site_id ], [ 'store_id', '=', $data[ 'delivery' ][ 'store_id' ] ] ])[ 'data' ];
                $shop_goods[ "local_config" ][ 'info' ] = $local_info;
            } else {
                $shop_goods[ 'local_config' ][ 'info' ] = [];
            }

            $delivery_array = $data[ 'delivery' ] ?? [];
            $delivery_type = $delivery_array[ "delivery_type" ] ?? 'express';
            if ($delivery_type == "store") {
                if (isset($data[ 'delivery' ][ "delivery_type" ]) && $data[ 'delivery' ][ "delivery_type" ] == "store") {
                    //门店自提
                    $delivery_money = 0;
                    $shop_goods[ 'delivery' ][ 'delivery_type' ] = 'store';
                    if ($shop_goods[ "store_config" ][ "is_use" ] == 0) {
                        $this->error = 1;
                        $this->error_msg = "门店自提方式未开启!";
                    }
                    if (empty($data[ 'delivery' ][ "store_id" ])) {
                        $this->error = 1;
                        $this->error_msg = "门店未选择!";
                    }
                    $shop_goods[ 'delivery' ][ 'store_id' ] = $data[ 'delivery' ][ "store_id" ];
                    $shop_goods[ 'buyer_ask_delivery_time' ] = $data[ 'buyer_ask_delivery_time' ];
                    $shop_goods = $this->storeOrderData($shop_goods, $data);
                }
            } else {
                if (empty($data[ 'member_address' ])) {
                    $delivery_money = 0;
                    $shop_goods[ 'delivery' ][ 'delivery_type' ] = 'express';

                    $this->error = 1;
                    $this->error_msg = "未配置默认收货地址!";
                } else {
                    if (!isset($data[ 'delivery' ][ "delivery_type" ]) || $data[ 'delivery' ][ "delivery_type" ] == "express") {
                        if ($shop_goods[ "express_config" ][ "is_use" ] == 1) {
                            //物流配送
                            $express = new Express();
                            $express_fee_result = $express->calculate($shop_goods, $data);
                            if ($express_fee_result[ "code" ] < 0) {
                                $this->error = 1;
                                $this->error_msg = $express_fee_result[ "message" ];
                                $delivery_fee = 0;
                            } else {
                                $delivery_fee = $express_fee_result[ 'data' ][ 'delivery_fee' ];
                            }
                        } else {
                            $this->error = 1;
                            $this->error_msg = "物流配送方式未开启!";
                            $delivery_fee = 0;
                        }
                        $delivery_money = $delivery_fee;
                        $shop_goods[ 'delivery' ][ 'delivery_type' ] = 'express';
                    } else if ($data[ 'delivery' ][ "delivery_type" ] == "local") {
                        //外卖配送
                        $delivery_money = 0;
                        $shop_goods[ 'delivery' ][ 'delivery_type' ] = 'local';
                        if ($shop_goods[ "local_config" ][ "is_use" ] == 0) {
                            $this->error = 1;
                            $this->error_msg = "外卖配送方式未开启!";
                        } else {
                            if (empty($data[ 'delivery' ][ "store_id" ])) {
                                $this->error = 1;
                                $this->error_msg = "门店未选择!";
                            }
                            $local_delivery_time = 0;
                            if (!empty($data[ 'buyer_ask_delivery_time' ])) {
                                $local_delivery_time = $data[ 'buyer_ask_delivery_time' ];
                            }
                            $shop_goods[ 'buyer_ask_delivery_time' ] = $local_delivery_time;

                            $local_model = new Local();
                            $local_result = $local_model->calculate($shop_goods, $data);

                            $shop_goods[ 'delivery' ][ 'start_money' ] = 0;
                            if ($local_result[ 'code' ] < 0) {
                                $this->error = $local_result[ 'data' ][ 'code' ];
                                $this->error_msg = $local_result[ 'message' ];
                                $shop_goods[ 'delivery' ][ 'start_money' ] = $local_result[ 'data' ][ 'start_money_array' ][ 0 ] ?? 0;
                            } else {
                                $delivery_money = $local_result[ 'data' ][ 'delivery_money' ];
                                if (!empty($local_result[ 'data' ][ 'error_code' ])) {
                                    $this->error = $local_result[ 'data' ][ 'code' ];
                                    $this->error_msg = $local_result[ 'data' ][ 'error' ];
                                }
                            }

                            $shop_goods[ 'delivery' ][ 'error' ] = $this->error;
                            $shop_goods[ 'delivery' ][ 'error_msg' ] = $this->error_msg;
                        }
                    }
                }
            }
        }

        //尾款金额
        $final_money = $shop_goods[ 'goods_money' ] - $data[ 'presale_info' ][ 'presale_deposit' ] * $data[ 'num' ] - $promotion_money - $deduction_money + $delivery_money;
        $shop_goods[ 'order_money' ] = $order_money; //订单总金额

        //发票相关
        $shop_goods = $this->invoice($shop_goods, $data);

        $final_money = $final_money + $shop_goods[ 'invoice_money' ] + $shop_goods[ 'invoice_delivery_money' ];

        //理论上是多余的操作
        if ($final_money < 0) {
            $final_money = 0;
        }

        //总结计算
        $shop_goods[ 'goods_money' ] = $goods_money;
        $shop_goods[ 'delivery_money' ] = $delivery_money;
        $shop_goods[ 'adjust_money' ] = $adjust_money;
        $shop_goods[ 'promotion_money' ] = $promotion_money;
        $shop_goods[ 'final_money' ] = $final_money;
        $shop_goods[ 'balance_money' ] = $balance_money;
        $shop_goods[ 'pay_money' ] = $pay_money;
        $shop_goods[ 'order_money' ] = $order_money;

        $this->goods_money += $goods_money;
        $this->delivery_money += $delivery_money;
        $this->coupon_money += $coupon_money;
        $this->adjust_money += $adjust_money;
        $this->invoice_money += $shop_goods[ 'invoice_money' ];
        $this->invoice_delivery_money += $shop_goods[ 'invoice_delivery_money' ];
        $this->promotion_money += $promotion_money;
        $this->final_money += $final_money;
        $this->order_name = string_split($this->order_name, ",", $shop_goods[ "order_name" ]);
        //买家留言
        if (isset($data[ 'buyer_message' ]) && isset($data[ 'buyer_message' ])) {
            $item_buyer_message = $data[ 'buyer_message' ];
            $shop_goods[ "buyer_message" ] = $item_buyer_message;
        } else {
            $shop_goods[ "buyer_message" ] = '';
        }
        return $shop_goods;
    }

    /**
     * 获取立即购买商品信息
     * @param \app\model\order\unknown $data
     * @return \app\model\order\multitype|array
     */
    public function getShopGoodsList($data)
    {
        $join = [
            [
                'site ns',
                'ngs.site_id = ns.site_id',
                'inner'
            ]
        ];
        $field = 'sku_id, sku_name, sku_no, price, discount_price,cost_price, stock, volume, weight, sku_image, ngs.site_id, goods_state, is_virtual, is_free_shipping, shipping_template,goods_class, goods_class_name, goods_id, ns.site_name,sku_spec_format,goods_name,max_buy,min_buy';
        $sku_info = model('goods_sku')->getInfo([ [ 'sku_id', '=', $data[ 'sku_id' ] ], [ "ngs.site_id", "=", $data[ "site_id" ] ] ], $field, 'ngs', $join);
        if (empty($sku_info)) {
            return $this->error([], "不存在的商品!");
        }

        $price = $data[ "price" ];

        $sku_info[ 'num' ] = $data[ 'num' ];
        $goods_money = $price * $data[ 'num' ];
        $sku_info[ 'price' ] = $price;
        $sku_info[ 'goods_money' ] = $goods_money;
        $sku_info[ 'real_goods_money' ] = $goods_money;
        $sku_info[ 'coupon_money' ] = 0; //优惠券金额
        $sku_info[ 'promotion_money' ] = 0; //优惠金额
        $sku_info[ 'stock' ] = numberFormat($sku_info[ 'stock' ]);
        $goods_list[] = $sku_info;
        $shop_goods = [
            'goods_money' => $goods_money,
            'site_id' => $sku_info[ 'site_id' ],
            'site_name' => $sku_info[ 'site_name' ],
            'goods_list_str' => $sku_info[ 'sku_id' ] . ':' . $sku_info[ 'num' ],
            'goods_list' => $goods_list,
            'order_name' => $sku_info[ "sku_name" ],
            'goods_num' => $sku_info[ 'num' ],
            'limit_purchase' => [
                'goods_' . $sku_info[ 'goods_id' ] => [
                    'goods_id' => $sku_info[ 'goods_id' ],
                    'goods_name' => $sku_info[ "sku_name" ],
                    'num' => $sku_info[ 'num' ],
                    'max_buy' => $sku_info[ 'max_buy' ],
                    'min_buy' => $sku_info[ 'min_buy' ]
                ]
            ]
        ];
        return $shop_goods;
    }

    /**
     * 添加门店关注记录和减少门店商品库存
     * @param $data
     * @return array
     */
    public function addStoreMemberAndDecStock($data)
    {
        if (!empty($data[ 'delivery_store_id' ])) {
            //添加店铺关注记录
            $shop_member_model = new StoreMemberModel();
            $res = $shop_member_model->addStoreMember($data[ 'delivery_store_id' ], $data[ 'member_id' ]);
            if ($res[ "code" ] < 0) {
                return $res;
            }
            $store_goods_sku_model = new StoreGoodsSku();
            $stock_result = $store_goods_sku_model->decStock([ "store_id" => $data[ "delivery_store_id" ], "sku_id" => $data[ "sku_id" ], "stock" => $data[ "num" ] ]);
            if ($stock_result[ "code" ] < 0) {
                return $this->error('', '当前门店库存不足,请选择其他门店');
            }
        }
        return $this->success();
    }


    /************************************************** 定金支付 end *********************************************************************/

    /************************************************** 尾款支付 start *********************************************************************/

    /**
     * 订单计算（尾款）
     * @param unknown $data
     */
    public function finalCalculate($data)
    {
        $data = $this->initMemberAccount($data);//初始化会员账户
        //余额付款
        if ($data[ 'is_balance' ] > 0) {
            $this->member_balance_money = $data[ "member_account" ][ "balance_total" ] ?? 0;
        }
        //查询预售订单信息
        $presale_order_model = new PresaleOrder();
        $order_info_result = $presale_order_model->getPresaleOrderInfo([ [ "id", "=", $data[ "id" ] ], [ 'site_id', '=', $data[ 'site_id' ] ] ]);
        $order_info = $order_info_result[ "data" ];

        $data[ "order_info" ] = $order_info;

        //判断是否可以支付尾款
        if ($order_info[ "pay_start_time" ] > time()) {
            $this->error = 1;
            $this->error_msg = "尾款支付时间还未开始!";
        }
        if ($order_info[ "pay_end_time" ] < time()) {
            $this->error = 1;
            $this->error_msg = "尾款支付时间已过，已停止支付!";
        }

        //尾款总金额（尾款实际金额 + 发票 + 物流等）
        $order_money = $order_info[ 'final_money' ];

        //余额抵扣(判断是否使用余额)
        if ($this->member_balance_money > 0) {
            if ($order_money <= $this->member_balance_money) {
                $balance_money = $order_money;
            } else {
                $balance_money = $this->member_balance_money;
            }
        } else {
            $balance_money = 0;
        }
        $pay_money = $order_money - $balance_money;//计算出实际支付金额
        $this->member_balance_money -= $balance_money;//预减少账户余额
        $this->balance_money += $balance_money;//累计余额
        $is_use = 1;

        $this->pay_money += $pay_money;
        //总结计算
        $data[ 'balance_final_money' ] = $this->balance_money;
        $data[ 'pay_final_money' ] = $this->pay_money;
        $data[ 'is_use_balance' ] = $is_use;
        $data[ 'balance_money' ] = $this->balance_money;
        return $data;
    }


    /**
     * 尾款支付
     * @param $data
     * @return array
     */
    public function payfinalMoneyPresaleOrder($data)
    {
        //查询出会员相关信息
        $calculate_data = $this->finalCalculate($data);

        if (isset($calculate_data[ 'code' ]) && $calculate_data[ 'code' ] < 0)
            return $calculate_data;

        if ($this->error > 0) {
            return $this->error([ 'error_code' => $this->error ], $this->error_msg);
        }

        $pay = new Pay();
        $out_trade_no = $pay->createOutTradeNo();

        $order_data = [
            'balance_final_money' => $calculate_data[ 'balance_final_money' ],
            'pay_final_money' => $calculate_data[ 'pay_final_money' ],
            'final_out_trade_no' => $out_trade_no,
        ];

        model('promotion_presale_order')->startTrans();
        try {

            model('promotion_presale_order')->update($order_data, [ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'id', '=', $data[ 'id' ] ] ]);

            //扣除余额(统一扣除)
            if ($calculate_data[ 'is_use_balance' ] == 1) {

                if ($calculate_data[ "balance_final_money" ] > 0) {

                    $calculate_data[ 'order_id' ] = $data[ 'id' ];
                    $this->pay_type = "BALANCE";
                    $balance_result = $this->useBalance($calculate_data, $data[ 'site_id' ], 'presale_order');
                    if ($balance_result[ "code" ] < 0) {
                        model("promotion_presale_order")->rollback();
                        return $balance_result;
                    }
                }
            }

            $order_name = $calculate_data[ 'order_info' ][ 'sku_name' ];
            //生成整体支付单据
            $pay->addPay($data[ 'site_id' ], $out_trade_no, $this->pay_type, $order_name, $order_name, $this->pay_money, '', 'FinalOrderPayNotify', '');

            model('promotion_presale_order')->commit();
            return $this->success($out_trade_no);
        } catch (\Exception $e) {
            model()->rollback();
            return $this->error('', $e->getMessage() . $e->getFile() . $e->getLine());
        }

    }

    /************************************************** 尾款支付 end *********************************************************************/

}